﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace nl.hanze.i.web.t41
{
    class Populatie
    {
        List<Mens> mensen;
        /*
         * Default constructor
         */
        public Populatie() {}

        /*
         * Constructor wich demands a list of Mens
         */
        public Populatie(List<Mens> mensen)
        {
            this.mensen = mensen;
        }

        /*
         * Add's a Mens to the mensen List
         */
        public void voegToe(Mens mens)
        {
            mensen.Add(mens);
        }

        /*
         * Removes a Mens from List mensen.
         * @param long bsn Bsn of Mens that needs to be deleted
         */
        public Boolean verwijder(long bsn)
        {
            foreach (Mens m in mensen)
            {
                if (long.Parse(m.Bsn) == bsn)
                {
                    mensen.Remove(m);
                    return true;
                } 
            }
            return false;
        }

        /*
         * Get's all women from the List mensen.
         */
        public List<Mens> geefVrouwen()
        {
            List<Mens> list = new List<Mens>();

            foreach (Mens m in mensen)
            {
                if (m.Geslacht == Sexe.Vrouw)
                {
                    list.Add(m);
                }
            }            
            return list;
        }

        /*
         * Get's all men from the List mensen.
         */
        public List<Mens> geefMannen()
        {
            List<Mens> list = new List<Mens>();

            foreach (Mens m in mensen)
            {
                if (m.Geslacht == Sexe.Man)
                {
                    list.Add(m);
                }
            }
            return list;
        }

        /*
         * Get's all Mens from List mensen, sorted by birthday.
         */
        public List<Mens> GeefVanJongNaarOud()
        {
            List<Mens> list = mensen;
            list.Sort(delegate(Mens m1, Mens m2) { return m1.Geboortedatum.CompareTo(m2.Geboortedatum); });
            return list;
        }

        /*
         * Override the ToString method.
         */
        public override String ToString() 
        {
            List<Mens> mensenInPopulatie = this.mensen;

            StringBuilder sb = new StringBuilder();

            foreach (Mens m in mensenInPopulatie)
            {
                String naam = m.Naam;
                String bsn = m.Bsn;

                sb.Append(" - Naam: " + naam + " ; BSN: " + bsn);
            }

            return sb.ToString();
        }

        /*
         * Saves all data to a file
         */
        public void SlaDataOp(String filename)
        {
            try
            {
                //Pass the filepath and filename to the StreamWriter Constructor
                StreamWriter sw = new StreamWriter(filename);

                foreach (Mens m in mensen)
                {
                    //Write a line of text
                    sw.WriteLine(m.Naam + "#" + geboorteDatumString(m.Geboortedatum) + "#" + m.Bsn + "#" + getSexeShort(m.Geslacht));
                }
                
                //Close the file
                sw.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }

        }

        /*
         * Returns a String value of supllied DateTime
         */
        private String geboorteDatumString(DateTime date)
        {
            String datum = String.Format("{0: dd-MM-yyyy}", date);

            return datum;
        }

        /*
         * Get abbreviation of the sexe
         */
        private String getSexeShort(Sexe s)
        {
            String output;

            if (s == Sexe.Man)
                output = "M";
            else
                output = "V";

            return output;
        }

        /**
         * Retrieve's all data from a specified file
         */
        public List<Mens> laadData(String filename)
        {
            String line;
            List<Mens> mensen = new List<Mens>();
            try
            {
                //Pass the file path and file name to the StreamReader constructor
                StreamReader sr = new StreamReader(filename);

                //Read the first line
                line = sr.ReadLine();

                // Continue to read until file ends
                while (line != null)
                {
                    // Parse Mens from line
                    Mens m = lineToMens(line);

                    // Add Mens to list
                    mensen.Add(m);

                    // Read the next line
                    line = sr.ReadLine();
                }

                // Close file
                sr.Close();

                // Return list
                return mensen;
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
                return null;
            }
        }

        /**
         * Parses Mens from a text line. 
         * Provided line syntax: naam#geb.datum#bsn#sexe
         */
        private Mens lineToMens(String line)
        {
            // Make new Mens
            Mens m = new Mens();

            // Read line
            string[] split          = line.Split(new Char[] { '#' });
            String naam             = split[0];
            DateTime geboortedatum  = DateTime.Parse(split[1]);
            String bsn              = split[2];
            Sexe s;
            if (split[3] == "M")
                s = Sexe.Man;
            else
                s = Sexe.Vrouw;


            // Assign values to Mens
            m.Naam = naam;
            m.Geboortedatum = geboortedatum;
            m.Bsn = bsn;
            m.Geslacht = s;

            return m;
        }
    }
}
